package com.landmesure.settings.fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.entrance.FragmentUpdate;
import com.landmesure.bluetoothchat.R;
import com.landmesure.settings.SettingActivity;

public class ResultInfoFragment extends Fragment implements FragmentUpdate.UpdateFragment {
    private static final String TAG = "ResultInfoFragment";
    private Context mContext;

    private MyHandler mHandler;
    private String mString;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_result_info, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        TextView textView = ((SettingActivity) mContext).findViewById(R.id.reset_result);
        textView.setText(mString);
        mHandler = new MyHandler(textView);
    }

    @Override
    public void update(Object type) {
        mString = (String) type;
        if (null != mHandler) {
            Message message = Message.obtain();
            message.what = 0;
            message.obj = mString;
            mHandler.sendMessage(message);
        }
    }

    private static final class MyHandler extends Handler {
        private TextView mTextView;
        private MyHandler(TextView textView) {
            mTextView = textView;
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mTextView.setText((String) msg.obj);
                    break;
                default:
                    break;
            }
        }
    }
}
